
import helpers from '../src/helpers'
// import { JSDOM } from 'jsdom'

// const { window } = new JSDOM(`<div id="dum-id" ></div>`);

// // 将被测函数需要用到的变量挂到全局
// global.localStorage = window.localStorage;

describe('常用工具函数单元测试', () => {

    it('dataUrlToBlob test', () => {
        const pngBase64 = '';
        const b = helpers.dataUrlToBlob(pngBase64);
        expect(b.type).toEqual("image/png")
    })

    it('formatDate test', () => {
        expect(helpers.formatDate(new Date('2023/02/01'), 'yyyy年MM月dd日')).toEqual('2023年02月01日')
        expect(helpers.formatDate('2023/02/01', 'dd MM yy')).toEqual('01 02 23')
        expect(helpers.formatDate('2023-02-1', 'dd MM yy')).toEqual('01 02 23')
        expect(helpers.formatDate('20230210', 'yyyyMMdd')).toEqual('20230210')
    });


    it('setStorage test', () => {

        expect.assertions(6);

        // 测试保存缓存数据
        helpers.setStorage('N_KEY', '测试串')
        expect(helpers.getStorage('N_KEY')).toEqual('测试串')

        try {
            // 测试重复设置缓存
            helpers.setStorage('N_KEY', '测试串2')
        } catch (error) {
            expect(error).toBeInstanceOf(Error); // 期望抛出的异常类型
        }

        try {
            // 更新不存在的缓存
            helpers.updateStorage('N_KEY1', '测试串2')
        } catch (error) {
            expect(error).toBeInstanceOf(Error); // 期望抛出的异常类型
        }

        // 测试更新缓存
        helpers.updateStorage('N_KEY', '测试串2', 2)

        expect(helpers.getStorage('N_KEY')).toEqual('测试串2')

        // 测试读取过期缓存
        helpers.updateStorage('N_KEY', '测试串2', 0)
        expect(helpers.getStorage('N_KEY')).toEqual(null)

        // 测试读取已有缓存
        localStorage.setItem('BN',45)
        expect(helpers.getStorage('BN')).toEqual(45)

    });

    it('nanoid test', () => {
        expect(helpers.nanoid().length).toEqual(21)
        expect(helpers.nanoid(10).length).toEqual(10)
    })

    it('parseQueryString test', () => {
        // expect(helpers.parseQueryString()).toEqual({dev:'1', isprd:'0'})
        expect(helpers.parseQueryString('dev', '?dev=1&isprd=0')).toBe('1')
        expect(helpers.parseQueryString('name', '?dev=1&isprd=0')).toBe('')
        expect(helpers.parseQueryString('dev', '?dev=1&dev=2&dev=3&isprd=0')).toEqual(['1','2','3'])
    })

    it('isWechat test', () => {
        expect(helpers.isWechat()).toBe(false)
    })

    it('isAndroid test', () => {
        expect(helpers.isAndroid()).toBe(false)
    })

    it('isIos test', () => {
        expect(helpers.isIos()).toBe(false)
    })

    it('isWeChatMiniApp test', () => {
        expect.assertions(1);
        helpers.isWeChatMiniApp().catch(e=>{
            expect(e).toBe(false)
        })
    });

});